﻿using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using ActorID = System.Guid;

namespace NIF.MobileActors.Core
{
    public class ActorManager
    {
        private static ActorManager s_instance = new ActorManager();
        private ConcurrentDictionary<ActorID, IActor> actors = new ConcurrentDictionary<ActorID, IActor>();

        public static ActorManager Instance
        {
            get
            {
                return s_instance;
            }
        }

        public IActor CreateActor(Type aType)
        {
            IActor actor = Activator.CreateInstance(aType, true) as IActor;
            System.Diagnostics.Debug.Assert(actor != null);

            this.Add(actor.ID, actor);
            return actor;
        }

        private void Add(ActorID actId, IActor actor)
        {
            actors.AddOrUpdate(actId, actor, (id, v) => v);
        }

        public ActorProxy<T> GetById<T>(ActorID id)
        {
            return new ActorProxy<T>(actors[id] as ActorBase<T>);
        }
    }
}
